using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using BattleCity.GameLogic;
using BattleCity.Rendering;

namespace BattleCity
{
    public static class ComponentManager
    {
        public static void onStart(this SpaceTankGame game)
        {
            Options.graphicsManager = game.graphics;
            Options.loadOptions();

            Scores.Initialize();
            Sound.Intialize(game.Content);

            if(Options.tomboneState == CurrentGameState.Menu)
                game.switchToMainMenu(false);

            if (Options.tomboneState == CurrentGameState.SingleLevel)
                game.switchToSingleLevelGamePlay(true, null);

            if (Options.tomboneState == CurrentGameState.Survival)
                game.switchToGamePlay(true, false);
        }

        public static void switchToMainMenu(this SpaceTankGame game, bool skipLogo)
        {
            Options.tomboneState = CurrentGameState.Menu;

            game.clearComponents();

            Sound.startMenuMusic();
            Sound.stopSoundsFx();

            MenuManager mainMenu = new MenuManager(game, skipLogo);

            mainMenu.survivalContinue = () => game.switchToGamePlay(true, false);
            mainMenu.survivalStart = () => game.switchToGamePlay(false, false);
            mainMenu.singleLevelContinue = () => game.switchToSingleLevelGamePlay(true, null);
            mainMenu.singleLevelStart = (levelNumber) => game.switchToSingleLevelGamePlay(false, levelNumber);
            game.Components.Add(mainMenu);
        }

        public static void switchToGamePlay(this SpaceTankGame game, bool gameContinue, bool nextLevel)
        {
            Options.tomboneState = CurrentGameState.Survival;

            game.clearComponents();

            if (!nextLevel)
            {
                Sound.startGamePlayMusic();
            }

            game.gameLogic = new GameStateUpdater(game, gameContinue, nextLevel);
            game.renderer = new Renderer(game);

            game.gameLogic.mainMenu = () => game.switchToMainMenu(true);
            game.gameLogic.nextLevel = () => game.switchToGamePlay(false, true);
            game.gameLogic.saveScore = () => game.showScoreKeyboard();
            
            game.Components.Add(game.gameLogic);
            game.Components.Add(game.renderer);
        }

        public static void switchToSingleLevelGamePlay(this SpaceTankGame game, bool gameContinue, int? levelNumber)
        {
            Options.tomboneState = CurrentGameState.SingleLevel;

            game.clearComponents();

            Sound.startGamePlayMusic();

            game.gameLogic = new GameStateUpdater(game, levelNumber, gameContinue);
            game.renderer = new Renderer(game);

            game.gameLogic.mainMenu = () => game.switchToMainMenu(true);

            game.Components.Add(game.gameLogic);
            game.Components.Add(game.renderer);
        }

        public static void clearComponents(this SpaceTankGame game)
        {
            while (game.Components.Count > 0)
            {
                ((GameComponent)game.Components[0]).Dispose();
            }

            if (game.renderer != null)
            {
                game.renderer.Dispose();
                game.renderer = null;
            }

            if (game.gameLogic != null)
            {
                game.gameLogic.Dispose();
                game.gameLogic = null;
            }

            game.Components.Clear();
        }
    }
}
